package com.tydic.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tydic.trans.beans.domain.Customer;
import com.tydic.trans.beans.domain.Provider;
import com.tydic.service.WebServiceDemoService;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
@WebService(serviceName = "WebServiceDemoService", // 与接口中指定的name一致
        targetNamespace = "http://service.tydic.com", // 与接口中的命名空间一致,一般是接口的包名倒序
        endpointInterface = "com.tydic.service.WebServiceDemoService" // 接口地址
)
public class WebServiceDemoServiceImpl implements WebServiceDemoService {

    @Override
    public String testString(String name) {
        return "hello, " + name;
    }

    @Override
    public Provider testObject(Customer customer) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" + customer);

        Provider provider = new Provider();
        provider.setSuccess(true);
        provider.setName("WebServiceDemoService");
        provider.setTargetNamespace("http://service.tydic.com");
        provider.setEndpointInterface("com.tydic.service.WebServiceDemoService");
        provider.setWsdl("http://localhost:8082/srti/api?wsdl");
        provider.setMessage(customer.getMessage() + ", 成功！");
        return provider;
    }

    @Override
    public Map<String, Object> testJson(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> map = objectMapper.readValue(json, Map.class);
            System.out.println(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "调用成功！");
        return result;
    }

    @Override
    public String testJsonStr(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> map = objectMapper.readValue(json, Map.class);
            System.out.println(map);

            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "调用成功！");

            return objectMapper.writeValueAsString(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }
}
